﻿using HLStock.Common;
using HLStock.Model;
using HLStock.WmsService;

namespace HLStock.DBAccess.AOP
{
    public class StockOutLimitCheckAction : IBeforeSaveAction<StockRecordModel>
    {
        public void Handle(BeforeSaveEventArgs<StockRecordModel> arg)
        {
            if (arg.Data.Type == StockType.Out)
            {
                var sp = ServiceLoader.GetService<StockRecordServiceProvider>();
                var inRecord = sp.Get(arg.Data.SrcRecord);
                var outRecords = sp.ListOutRecord(arg.Data.SrcRecord);
                var o = outRecords.Where(r => r.Pk != arg.Data.Pk).ToList();
                o.Add(arg.Data);
                if (o.Sum(r => r.Qty) > inRecord.Qty)
                {
                    arg.Cancle = true;
                    arg.Message = "已经超限出库";
                    return;
                }
            }
        }
    }
}
